package com.jiyi.activity.datainput.sm;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiyi.bean.datainput.new_input.BaseCheck;
import com.jiyi.bean.datainput.new_input.FireEquipBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.retrofit.HttpMethods;
import com.jiyi.widget.MultiLineRadioGroup;
import com.jiyi.widget.NoScrollGridView;
import com.jiyi.widget.picselect.PicsAdapter;
import com.jiyi.widget.picshow.ImagePagerActivity;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.utils.UserInfoUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/30.
 */

public class FireEquipReadActivity extends ActivityBase {


    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.ivHint)
    ImageView ivHint;
    @BindView(R.id.top_layout)
    RelativeLayout topLayout;
    @BindView(R.id.rgEquipWorkIsOK)
    MultiLineRadioGroup rgEquipWorkIsOK;
    @BindView(R.id.tvEquipWorkProblem)
    TextView tvEquipWorkProblem;
    @BindView(R.id.tvEquipWorkMeasure)
    TextView tvEquipWorkMeasure;
    @BindView(R.id.rgAutoWaterSysIsOk)
    MultiLineRadioGroup rgAutoWaterSysIsOk;
    @BindView(R.id.tvAutoWaterSysProblem)
    TextView tvAutoWaterSysProblem;
    @BindView(R.id.tvAutoWaterSysMeasure)
    TextView tvAutoWaterSysMeasure;
    @BindView(R.id.rgAutoAlarmSysIsOk)
    MultiLineRadioGroup rgAutoAlarmSysIsOk;
    @BindView(R.id.tvAutoAlarmSysProblem)
    TextView tvAutoAlarmSysProblem;
    @BindView(R.id.tvAutoAlarmSysMeasure)
    TextView tvAutoAlarmSysMeasure;
    @BindView(R.id.rgHydrantIsOk)
    MultiLineRadioGroup rgHydrantIsOk;
    @BindView(R.id.tvHydrantProblem)
    TextView tvHydrantProblem;
    @BindView(R.id.tvHydrantMeasure)
    TextView tvHydrantMeasure;
    @BindView(R.id.rgSmokeControlIsOk)
    MultiLineRadioGroup rgSmokeControlIsOk;
    @BindView(R.id.tvSmokeControlProblem)
    TextView tvSmokeControlProblem;
    @BindView(R.id.tvSmokeControlMeasure)
    TextView tvSmokeControlMeasure;
    @BindView(R.id.rgBroadcastIsOk)
    MultiLineRadioGroup rgBroadcastIsOk;
    @BindView(R.id.tvBroadcastProblem)
    TextView tvBroadcastProblem;
    @BindView(R.id.tvBroadcastMeasure)
    TextView tvBroadcastMeasure;
    @BindView(R.id.rgLightingIsOk)
    MultiLineRadioGroup rgLightingIsOk;
    @BindView(R.id.tvLightingProblem)
    TextView tvLightingProblem;
    @BindView(R.id.tvLightingMeasure)
    TextView tvLightingMeasure;
    @BindView(R.id.rgFireLiftIsOk)
    MultiLineRadioGroup rgFireLiftIsOk;
    @BindView(R.id.tvFireLiftProblem)
    TextView tvFireLiftProblem;
    @BindView(R.id.tvFireLiftMeasure)
    TextView tvFireLiftMeasure;
    @BindView(R.id.rbOtherYes)
    RadioButton rbOtherYes;
    @BindView(R.id.rbOtherMalfunction)
    RadioButton rbOtherMalfunction;
    @BindView(R.id.rbOtherDamage)
    RadioButton rbOtherDamage;
    @BindView(R.id.rbOtherParalysis)
    RadioButton rbOtherParalysis;
    @BindView(R.id.rgOtherIsOk)
    RadioGroup rgOtherIsOk;
    @BindView(R.id.tvOtherProblem)
    TextView tvOtherProblem;
    @BindView(R.id.tvOtherMeasure)
    TextView tvOtherMeasure;

    String build_id;

    @BindView(R.id.gvEquipWork)
    NoScrollGridView gvEquipWork;
    @BindView(R.id.gvAutoWaterSys)
    NoScrollGridView gvAutoWaterSys;
    @BindView(R.id.gvAutoAlarmSys)
    NoScrollGridView gvAutoAlarmSys;
    @BindView(R.id.gvHydrant)
    NoScrollGridView gvHydrant;
    @BindView(R.id.gvSmokeControl)
    NoScrollGridView gvSmokeControl;
    @BindView(R.id.gvBroadcast)
    NoScrollGridView gvBroadcast;
    @BindView(R.id.gvLighting)
    NoScrollGridView gvLighting;
    @BindView(R.id.gvFireLift)
    NoScrollGridView gvFireLift;
    @BindView(R.id.gvOther)
    NoScrollGridView gvOther;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fire_equip_read);
        ButterKnife.bind(this);
        init();
    }

    public void init() {
        getIntentData();
        setTop();
        getData();
    }

    private void getIntentData() {
        build_id = getIntent().getStringExtra("build_no");
        if (TextUtils.isEmpty(build_id))
            build_id = "";
    }

    private void setTop() {
        tvTitle.setText("消防设施设备是否完好有效");
        tvSearch.setCompoundDrawables(null, null, null, null);
        tvSearch.setText("保存");
    }


    public void getData() {
        HttpMethods.getInstance().getFireEquipInfo(UserInfoUtil.getCurrentUserAccessToken(), build_id, 2)
                .subscribe(new SampleProgressObserver<FireEquipBean>(this) {
                    @Override
                    public void onNext(FireEquipBean fireEquipBean) {
                        if (fireEquipBean == null || "".equals(fireEquipBean.getGson_null())) {
                            fireEquipBean = new FireEquipBean();
                            BaseCheck bc1 = new BaseCheck();
                            bc1.setIs_ok(1);
                            bc1.setProblem("");
                            bc1.setMeasures("");
                            fireEquipBean.setControl_room_equipment(bc1);
                            BaseCheck bc2 = new BaseCheck();
                            bc2.setIs_ok(1);
                            bc2.setProblem("");
                            bc2.setMeasures("");
                            fireEquipBean.setSprinkler_system(bc2);
                            BaseCheck bc3 = new BaseCheck();
                            bc3.setIs_ok(1);
                            bc3.setProblem("");
                            bc3.setMeasures("");
                            fireEquipBean.setFire_alarm_system(bc3);
                            BaseCheck bc4 = new BaseCheck();
                            bc4.setIs_ok(1);
                            bc4.setProblem("");
                            bc4.setMeasures("");
                            fireEquipBean.setInside_hydrant(bc4);
                            BaseCheck bc5 = new BaseCheck();
                            bc5.setIs_ok(1);
                            bc5.setProblem("");
                            bc5.setMeasures("");
                            fireEquipBean.setMechanical_smoke_control(bc5);
                            BaseCheck bc6 = new BaseCheck();
                            bc6.setIs_ok(1);
                            bc6.setProblem("");
                            bc6.setMeasures("");
                            fireEquipBean.setEmergent_broadcast(bc6);
                            BaseCheck bc7 = new BaseCheck();
                            bc7.setIs_ok(1);
                            bc7.setProblem("");
                            bc7.setMeasures("");
                            fireEquipBean.setLighting_evacuation_indicator(bc7);
                            BaseCheck bc8 = new BaseCheck();
                            bc8.setIs_ok(1);
                            bc8.setProblem("");
                            bc8.setMeasures("");
                            fireEquipBean.setFire_elevator(bc8);
                            BaseCheck bc9 = new BaseCheck();
                            bc9.setIs_ok(1);
                            bc9.setProblem("");
                            bc9.setMeasures("");
                            fireEquipBean.setOther(bc9);
                        }

                        fillData(fireEquipBean);
                    }
                });
    }

    private void fillData(FireEquipBean bean) {
        if (bean.getControl_room_equipment().getIs_ok() == 1) {
            rgEquipWorkIsOK.setItemChecked(0);
        } else if (bean.getControl_room_equipment().getIs_ok() == 2) {
            rgEquipWorkIsOK.setItemChecked(1);
        } else if (bean.getControl_room_equipment().getIs_ok() == 3) {
            rgEquipWorkIsOK.setItemChecked(2);
        } else if (bean.getControl_room_equipment().getIs_ok() == 4) {
            rgEquipWorkIsOK.setItemChecked(3);
        } else {
            rgEquipWorkIsOK.setItemChecked(4);
        }

        tvEquipWorkProblem.setText(bean.getControl_room_equipment().getProblem());
        tvEquipWorkMeasure.setText(bean.getControl_room_equipment().getMeasures());

        if (bean.getSprinkler_system().getIs_ok() == 1) {
            rgAutoWaterSysIsOk.setItemChecked(0);
        } else if (bean.getSprinkler_system().getIs_ok() == 2) {
            rgAutoWaterSysIsOk.setItemChecked(1);
        } else if (bean.getSprinkler_system().getIs_ok() == 3) {
            rgAutoWaterSysIsOk.setItemChecked(2);
        } else if (bean.getSprinkler_system().getIs_ok() == 3) {
            rgAutoWaterSysIsOk.setItemChecked(3);
        } else {
            rgAutoWaterSysIsOk.setItemChecked(4);
        }

        tvAutoWaterSysProblem.setText(bean.getSprinkler_system().getProblem());
        tvAutoWaterSysMeasure.setText(bean.getSprinkler_system().getMeasures());

        if (bean.getFire_alarm_system().getIs_ok() == 1) {
            rgAutoAlarmSysIsOk.setItemChecked(0);
        } else if (bean.getFire_alarm_system().getIs_ok() == 2) {
            rgAutoAlarmSysIsOk.setItemChecked(1);
        } else if (bean.getFire_alarm_system().getIs_ok() == 3) {
            rgAutoAlarmSysIsOk.setItemChecked(2);
        } else if (bean.getFire_alarm_system().getIs_ok() == 4) {
            rgAutoAlarmSysIsOk.setItemChecked(3);
        } else {
            rgAutoAlarmSysIsOk.setItemChecked(4);
        }

        tvAutoAlarmSysProblem.setText(bean.getFire_alarm_system().getProblem());
        tvAutoAlarmSysMeasure.setText(bean.getFire_alarm_system().getMeasures());

        if (bean.getInside_hydrant().getIs_ok() == 1) {
            rgHydrantIsOk.setItemChecked(0);
        } else if (bean.getInside_hydrant().getIs_ok() == 2) {
            rgHydrantIsOk.setItemChecked(1);
        } else if (bean.getInside_hydrant().getIs_ok() == 3) {
            rgHydrantIsOk.setItemChecked(2);
        } else if (bean.getInside_hydrant().getIs_ok() == 4) {
            rgHydrantIsOk.setItemChecked(3);
        } else {
            rgHydrantIsOk.setItemChecked(4);
        }

        tvHydrantProblem.setText(bean.getInside_hydrant().getProblem());
        tvHydrantMeasure.setText(bean.getInside_hydrant().getMeasures());

        if (bean.getMechanical_smoke_control().getIs_ok() == 1) {
            rgSmokeControlIsOk.setItemChecked(0);
        } else if (bean.getMechanical_smoke_control().getIs_ok() == 2) {
            rgSmokeControlIsOk.setItemChecked(1);
        } else if (bean.getMechanical_smoke_control().getIs_ok() == 3) {
            rgSmokeControlIsOk.setItemChecked(2);
        } else if (bean.getMechanical_smoke_control().getIs_ok() == 4) {
            rgSmokeControlIsOk.setItemChecked(3);
        } else {
            rgSmokeControlIsOk.setItemChecked(4);
        }

        tvSmokeControlProblem.setText(bean.getMechanical_smoke_control().getProblem());
        tvSmokeControlMeasure.setText(bean.getMechanical_smoke_control().getMeasures());

        if (bean.getEmergent_broadcast().getIs_ok() == 1) {
            rgBroadcastIsOk.setItemChecked(0);
        } else if (bean.getEmergent_broadcast().getIs_ok() == 2) {
            rgBroadcastIsOk.setItemChecked(1);
        } else if (bean.getEmergent_broadcast().getIs_ok() == 3) {
            rgBroadcastIsOk.setItemChecked(2);
        } else if (bean.getEmergent_broadcast().getIs_ok() == 3) {
            rgBroadcastIsOk.setItemChecked(3);
        } else {
            rgBroadcastIsOk.setItemChecked(4);
        }

        tvBroadcastProblem.setText(bean.getEmergent_broadcast().getProblem());
        tvBroadcastMeasure.setText(bean.getEmergent_broadcast().getMeasures());

        if (bean.getLighting_evacuation_indicator().getIs_ok() == 1) {
            rgLightingIsOk.setItemChecked(0);
        } else if (bean.getLighting_evacuation_indicator().getIs_ok() == 2) {
            rgLightingIsOk.setItemChecked(1);
        } else if (bean.getLighting_evacuation_indicator().getIs_ok() == 3) {
            rgLightingIsOk.setItemChecked(2);
        } else if (bean.getLighting_evacuation_indicator().getIs_ok() == 3) {
            rgLightingIsOk.setItemChecked(3);
        } else {
            rgLightingIsOk.setItemChecked(4);
        }

        tvLightingProblem.setText(bean.getLighting_evacuation_indicator().getProblem());
        tvLightingMeasure.setText(bean.getLighting_evacuation_indicator().getMeasures());

        if (bean.getFire_elevator().getIs_ok() == 1) {
            rgFireLiftIsOk.setItemChecked(0);
        } else if (bean.getFire_elevator().getIs_ok() == 2) {
            rgFireLiftIsOk.setItemChecked(1);
        } else if (bean.getFire_elevator().getIs_ok() == 3) {
            rgFireLiftIsOk.setItemChecked(2);
        } else if (bean.getFire_elevator().getIs_ok() == 3) {
            rgFireLiftIsOk.setItemChecked(3);
        } else {
            rgFireLiftIsOk.setItemChecked(4);
        }

        tvFireLiftProblem.setText(bean.getFire_elevator().getProblem());
        tvFireLiftMeasure.setText(bean.getFire_elevator().getMeasures());

        if (bean.getOther().getIs_ok() == 1) {
            rbOtherYes.setChecked(true);
        } else if (bean.getOther().getIs_ok() == 2) {
            rbOtherMalfunction.setChecked(true);
        } else if (bean.getOther().getIs_ok() == 3) {
            rbOtherDamage.setChecked(true);
        } else {
            rbOtherParalysis.setChecked(true);
        }

        tvOtherProblem.setText(bean.getOther().getProblem());
        tvOtherMeasure.setText(bean.getOther().getMeasures());

        initGridViewList(bean);

    }

    void initGridViewList(FireEquipBean fireEquipBean)
    {
        if (fireEquipBean.getControl_room_equipment().getImage() == null || fireEquipBean.getControl_room_equipment().getImage().size() <= 0) {
            gvEquipWork.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvEquipWork,fireEquipBean.getControl_room_equipment().getImage());
        }

        if (fireEquipBean.getSprinkler_system().getImage() == null || fireEquipBean.getSprinkler_system().getImage().size() <= 0) {
            gvAutoWaterSys.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvAutoWaterSys,fireEquipBean.getSprinkler_system().getImage());
        }

        if (fireEquipBean.getFire_alarm_system().getImage() == null || fireEquipBean.getFire_alarm_system().getImage().size() <= 0) {
            gvAutoAlarmSys.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvAutoAlarmSys,fireEquipBean.getFire_alarm_system().getImage());
        }

        if (fireEquipBean.getInside_hydrant().getImage() == null || fireEquipBean.getInside_hydrant().getImage().size() <= 0) {
            gvHydrant.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvHydrant,fireEquipBean.getInside_hydrant().getImage());
        }

        if (fireEquipBean.getMechanical_smoke_control().getImage() == null || fireEquipBean.getMechanical_smoke_control().getImage().size() <= 0) {
            gvSmokeControl.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvSmokeControl,fireEquipBean.getMechanical_smoke_control().getImage());
        }

        if (fireEquipBean.getEmergent_broadcast().getImage() == null || fireEquipBean.getEmergent_broadcast().getImage().size() <= 0) {
            gvBroadcast.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvBroadcast,fireEquipBean.getEmergent_broadcast().getImage());
        }

        if (fireEquipBean.getLighting_evacuation_indicator().getImage() == null || fireEquipBean.getLighting_evacuation_indicator().getImage().size() <= 0) {
            gvLighting.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvLighting,fireEquipBean.getLighting_evacuation_indicator().getImage());
        }

        if (fireEquipBean.getFire_elevator().getImage() == null || fireEquipBean.getFire_elevator().getImage().size() <= 0) {
            gvFireLift.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvFireLift,fireEquipBean.getFire_elevator().getImage());
        }

        if (fireEquipBean.getOther().getImage() == null || fireEquipBean.getOther().getImage().size() <= 0) {
            gvOther.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvOther,fireEquipBean.getOther().getImage());
        }

    }

    void setGridviewAdapter(GridView gridView, List<String> picList)
    {
        gridView.setVisibility(View.VISIBLE);
        //图片列表
        if (picList != null) {
            final String [] urls = new String [picList.size()];
            for (int i = 0; i < picList.size(); i++) {
                urls[i] = picList.get(i);
            }

            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent intent = new Intent(view.getContext(),ImagePagerActivity.class);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                    view.getContext().startActivity(intent);
                }
            });
            PicsAdapter picsAdapter = new PicsAdapter(gridView.getContext(),picList);
            gridView.setAdapter(picsAdapter);
        }
    }

    @OnClick({R.id.tvBack})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            default:
                break;
        }
    }
}
